# -*- coding: UTF-8 -*-

from exts import db
from werkzeug.security import generate_password_hash, check_password_hash


# 数据库表的定义
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    password = db.Column(db.String(50), nullable=False)
    nickname = db.Column(db.String(50), nullable=False)
    is_root = db.Column(db.Integer, default=0)

    def __repr__(self):
        return '<user %d %s %s %d>' % (self.id, self.password, self.nickname, self.isroot)

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password, password)
